package com.ruoyi.ftrl.service;

import com.ruoyi.common.config.MessageEventListeners;
import com.ruoyi.common.core.redis.RedisCache;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.ftrl.domain.ControlRecords;
import com.ruoyi.ftrl.domain.ControlValve;
import com.ruoyi.ftrl.mapper.ControlRecordsMapper;
import com.ruoyi.ftrl.mapper.ControlValveMapper;
import com.ruoyi.ftrl.service.impl.ControlValveServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class RedisMessageHandlers implements MessageEventListeners {
    @Autowired
    //Redis工具类
    RedisCache redisCache;
    @Autowired
    private ControlValveServiceImpl controlValveService;
    @Autowired
    private ControlRecordsMapper controlRecordsMapper;

    public static String message;

    public static void main(String[] args) {
       String string= "12";
        int i = Integer.parseInt(string);
        // 将十进制数转换为两位十六进制字符串
        String hexString = String.format("%02x", i);

        // 输出十六进制字符串
        System.out.println("The hexadecimal equivalent of decimal " + i + " is: " + hexString);
    }


    /**
     * 监听设备回执信息 进行解码（回执调控设备的成功或失败）
     * @param message
     */
    @Override
    public void onMessage(String message) {

        RedisMessageHandlers.message = message;
        System.err.println("Received message(监听到频道号------receipt------的消息为): " + message);
        String focusId = message.substring(2, 4)+message.substring(0,2);
        int i = Integer.parseInt(focusId, 16);
        focusId = String.valueOf(i);
        System.err.println("集中器ID为------"+focusId);
        String substring1 = message.substring(4, 6);
        int i1 = Integer.parseInt(substring1, 16);
        substring1=String.valueOf(i1);
        System.err.println("计量点ID为---------"+substring1);
        String substring = message.substring(6, 8);
        System.err.println("设备执行状态为--------"+substring);
        // todo 求出设备执行的成功失败 成功 修改表操作状态为成功  失败 修改操作状态为失败 并抛异常 弹窗显示那个房间调控失败
        ControlRecords controlRecords=controlRecordsMapper.selectControlRecordsByEId(focusId,substring1);
        //将设备调控状态赋值到操作记录中
        if (substring.equals("正确")){
            controlRecordsMapper.updateControlRecordsState(controlRecords.getId());
        }
//        else {
//            String room = controlRecords.getRoom();
//            controlRecordsMapper.updateControlRecordsStates(controlRecords.getId());
//            throw new ServiceException("房间"+room+"设备调控失败，请重新调控或者进行设备检查！");
//        }




//        controlValveService.completeFuture(message);


    }
}
